<template>
  <div class="image-upload">
    <el-upload
      class="avatar-uploader"
      action="https://jsonplaceholder.typicode.com/posts/"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      @mouseenter.native="showCover = imageUrl ? true : false"
      @mouseleave.native="showCover = false"
      :http-request="send"
    >
      <div class="progress_cover" v-if="showProgress">
        <el-progress type="circle" :percentage="percent"></el-progress>
      </div>
      <img v-else-if="imageUrl" :src="imageUrl" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      <!-- 进度条 -->

      <!-- 删除和预览阴影 -->
      <div class="handel_cover" v-if="showCover">
        <el-icon
          @click.native.stop="previewClick"
          class="el-icon-zoom-in"
        ></el-icon>
        <el-icon
          @click.native.stop="deleteClick"
          class="el-icon-delete"
        ></el-icon>
      </div>
    </el-upload>

    <el-dialog
      title="图片预览"
      :visible="showDialog"
      @close="showDialog = false"
    >
      <img :src="imageUrl" alt="" style="width:100%" />
    </el-dialog>
  </div>
</template>

<script>
//
import COS from "cos-js-sdk-v5";

// 设置腾讯云服务器
var cos = new COS({
  SecretId: "AKIDXrviFWWFixwe7KHxNXQ3QWNyJPEm45qK",
  SecretKey: "EDwPbWAXdR2MPAernSqxnMegQt62h5C1"
});
export default {
  name: "imageUpload",
  data() {
    return {
      imageUrl: "",
      showCover: false,
      showDialog: false,
      //   进度条
      percent: 0,
      showProgress: false
    };
  },
  methods: {
    //   预览
    previewClick() {
      this.showDialog = true;
    },
    // 删除
    deleteClick() {
      this.imageUrl = "";
      this.showCover = false;
    },
    //   上传成功的时候回调
    handleAvatarSuccess(res, file) {
      //   this.imageUrl = URL.createObjectURL(file.raw);
    },

    // 上传前的回调函数
    beforeAvatarUpload(file) {
      const isJPG =
        file.type === "image/jpeg" ||
        file.type === "image/gif " ||
        file.type === "image/jpg" ||
        file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    // 自定义上传
    send(params) {
      this.showProgress = true;
      cos.putObject(
        {
          // 腾讯云存储桶名称
          Bucket: "hmhr72-1306790611" /* 必须 */,
          Region: "ap-guangzhou" /* 存储桶所在地域，必须字段 */,
          //   保存的文件名
          Key: params.file.name /* 必须 */,
          //   这个是存储方式，不用修改
          StorageClass: "STANDARD",
          Body: params.file, // 上传文件对象
          // 是一个回调函数， progressData为上传进度
          onProgress: progressData => {
            this.percent = progressData.percent * 100;
          }
        },
        // 成功或失败回调的方法
        (err, data) => {
          if (!err) {
            this.imageUrl = "http://" + data.Location;
          }
          this.percent = 0;
          this.showProgress = false;
        }
      );
    }
  }
};
</script>

<style lang="scss">
.image-upload {
  width: 178px;
  height: 178px;
}
.handel_cover,
.progress_cover {
  position: absolute;
  left: 0;
  top: 0;
  width: 178px;
  height: 178px;
  background-color: rgba(0, 0, 0, 0.3);

  display: flex;
  justify-content: center;
  align-items: center;

  font-size: 30px;
  i {
    margin: 5px;
  }
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
